#include "pch.h"
#include <iostream>
using namespace std;
#include "Connection.h"
#include "CommonConnectionPool.h"

int main() {
	clock_t begin = clock();
	thread t1([]() {
		ConnectionPool* cp = ConnectionPool::getConnectionPool();
		for (int i = 0; i < 1250; i++) {
			char sql[1024] = { 0 };
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			shared_ptr<Connection> sp = cp->getConnection();
			sp->update(sql);
		}
		});
	thread t2([]() {
		ConnectionPool* cp = ConnectionPool::getConnectionPool();
		for (int i = 1250; i < 2500; i++) {
			char sql[1024] = { 0 };
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			shared_ptr<Connection> sp = cp->getConnection();
			sp->update(sql);
		}
		});
	thread t3([]() {
		ConnectionPool* cp = ConnectionPool::getConnectionPool();
		for (int i = 2500; i < 3750; i++) {
			char sql[1024] = { 0 };
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			shared_ptr<Connection> sp = cp->getConnection();
			sp->update(sql);
		}
		});
	thread t4([]() {
		ConnectionPool* cp = ConnectionPool::getConnectionPool();
		for (int i = 3750; i < 5000; i++) {
			char sql[1024] = { 0 };
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			shared_ptr<Connection> sp = cp->getConnection();
			sp->update(sql);
		}
		});
	t1.join();
	t2.join();
	t3.join();
	t4.join();
	clock_t end = clock();

	cout << (end - begin) << "ms" << endl;

	return 0;
}

/*
int main() {
	Connection conn;
	conn.connect("127.0.0.1", 3306, "root", "123456", "score");
	clock_t begin = clock();
	thread t1([]() {
		for (int i = 0; i < 1250; i++) {
			Connection conn;
			char sql[1024] = { 0 };
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			conn.connect("127.0.0.1", 3306, "root", "123456", "score");
			conn.update(sql);
		}
		});
	thread t2([]() {
		for (int i = 1250; i < 2500; i++) {
			Connection conn;
			char sql[1024] = { 0 };
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			conn.connect("127.0.0.1", 3306, "root", "123456", "score");
			conn.update(sql);
		}
		});
	thread t3([]() {
		for (int i = 2500; i < 3750; i++) {
			char sql[1024] = { 0 };
			Connection conn;
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			conn.connect("127.0.0.1", 3306, "root", "123456", "score");
			conn.update(sql);
		}
		});
	thread t4([]() {
		for (int i = 3750; i < 5000; i++) {
			char sql[1024] = { 0 };
			Connection conn;
			sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
				i, "123456");
			conn.connect("127.0.0.1", 3306, "root", "123456", "score");
			conn.update(sql);
		}
		});
	t1.join();
	t2.join();
	t3.join();
	t4.join();
	clock_t end = clock();

	cout << (end - begin) << "ms" << endl;

	return 0;
}*/

/*
int main()
{
	clock_t begin = clock();
	ConnectionPool* cp = ConnectionPool::getConnectionPool();
	for (int i = 0; i < 5000; ++i) {
		shared_ptr<Connection> sp = cp->getConnection();
		char sql[1024] = { 0 };
		sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
			i, "123456");
		sp->update(sql);
	}
	clock_t end = clock();

	cout << (end - begin) << "ms" << endl;
}
*/


/*
int main()
{
	clock_t begin = clock();
	for (int i = 0; i < 5000; ++i) {
		Connection conn;
		char sql[1024] = { 0 };
		sprintf(sql, "insert into admin(id,password) values(%d,'%s')",
			i, "123456");
		conn.connect("127.0.0.1", 3306, "root", "123456", "score");
		conn.update(sql);
	}
	clock_t end = clock();

	cout << (end - begin) << "ms" << endl;
}
*/





